﻿using KJSyncXP.Services;
using KJSyncXP.Sqlite.Model;
using KJSyncXP.Sqlite.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace KJSyncXP.ViewModels
{
    public class AuthorizationVM : BaseViewModel
    {
        User _currentUser;

        List<VUserpermission> _permissionList;
        AuthorizationService _authorizationService;

        public List<VUserpermission> Userpermission
        {
            get => _permissionList;
            set
            {
                _permissionList = value;
                RaisePropertyChanged("Userpermission");
            }
        }
        public List<string> Tokens
        { get; set; } = new List<string>();
        public User CurrentUser
        {
            get => _currentUser;
            set
            {
                if (_currentUser == value) return;
                _currentUser = value;
            }
        }

        public AuthorizationVM(AuthorizationService authorizationService)
        {
            _authorizationService = authorizationService;
        }
        /// <summary>
        /// 初始用户的权限。
        /// 
        /// </summary>
        public void init()
        {
            Userpermission = _authorizationService.GetPermissions(_currentUser.Useruuid);
            Tokens.Clear();
            foreach (var permission in Userpermission)
            {
                Tokens.Add(permission.Pkey);
            }
        }

        public void clear()
        {
            Userpermission.Clear();
            Tokens.Clear();
        }
    }
}
